<template lang="pug">
el-dialog(title="创建报损单" :visible.sync="dialog" :before-close="close" append-to-body)
    goodslist(@getgoodsid="getgooditem")
    el-row
        el-col(:span="24")
            el-button(type="primary" @click="save") 保存
            el-button(type="primary" @click="open") 选择报损商品
        el-col.mgt(:span="24")
            el-col(:span="2")
                p 报损日期
            el-col(:span="10")
                el-date-picker(v-model="datetime" type="date" placeholder="选择日期(必填)")
    el-row
        el-col(:span="24")
            el-table(:data="td" style="width: 100%" height="350" v-loading="loading" element-loading-text="数据加载中...")
                el-table-column(prop="goodsId" label="商品编号")
                el-table-column(prop="name" label="商品名称")
                el-table-column(label="成本单价/元")
                    template(slot-scope="scope")
                        el-input(type="number" v-model="scope.row.prices" placeholder="必填")
                el-table-column(prop="spec" label="报损单位")
                el-table-column(label="报损量")
                    template(slot-scope="scope")
                        el-input(type="number" v-model="scope.row.lossNum" placeholder="必填")
                el-table-column(label="报损金额/元")
                    template(slot-scope="scope")
                        el-input(type="number" v-model="scope.row.lossPrice" placeholder="必填")
                el-table-column(label="备注")
                    template(slot-scope="scope")
                        el-input(type="text" v-model="scope.row.remark" placeholder="选填")
                el-table-column(label="操作")
                    template(slot-scope="scope")
                        el-button(size="mini" type="danger" @click="del(scope.$index, scope.row)") 删除
</template>
<script>
import moment from 'moment'
import goodslist from 'components/GoodsLists'
export default{
    data(){
        return{
            dialog:false,
            loading:false,
            datetime:'',
            td:[]
        }
    },
    components:{
        goodslist
    },
    computed:{
        getstate(){
            return this.$store.state.createbaosunclass;
        }
    },
    watch:{
        getstate(val){
            this.dialog=val
        }
    },
    methods:{
        checkall(){//遍历所有填写的地方,效验
            if(!this.datetime){
                this.$message.error('报损时间不能为空')
                return false
            }
            for(var i=0;i<this.td.length;i++){
                if(!this.td[i].prices){
                    this.$message.error('成本价不能为空')
                    return false
                }
                if(this.td[i].prices<=0){
                    this.$message.error('成本价必须大于0')
                    return false
                }
                if(!this.td[i].spec){
                    this.$message.error('单位不能为空')
                    return false
                }
                if(!this.td[i].lossNum){
                    this.$message.error('报损数量不能为空')
                    return false
                }
                if(this.td[i].lossNum<=0){
                    this.$message.error('报损数量必须大于0')
                    return false
                }
                if(!this.td[i].lossPrice){
                    this.$message.error('报损金额不能为空')
                    return false
                }
                if(this.td[i].lossPrice<=0){
                    this.$message.error('报损金额必须大于0')
                    return false
                }
            }
            return true
        },
        checkacllback(arr){//遍历请求的返回值
            for(var i=0;i<arr.length;i++){
                if(arr[i].data.code!=200){
                    this.$message.error(arr[i].data.message)
                    return false
                }
                return true
            }
        },
        save(){//保存
            var _this=this
            if(this.$store.state.TOPshopID){
                
            }else{
                _this.$message.info('权限有误,不能添加')
                return
            }
            if(!this.checkall()){
                return
            }
            this.$confirm('创建后无法修改,是否确定?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var axiosList=new Array()
                for(var i=0;i<_this.td.length;i++){
                    var pp = new URLSearchParams()
                    pp.append('goodId',_this.td[i].goodsId)
                    pp.append('goodName',_this.td[i].name)
                    pp.append('spec',_this.td[i].spec)
                    pp.append('price',_this.td[i].prices)
                    pp.append('lossNum',_this.td[i].lossNum)
                    pp.append('lossPrice',_this.td[i].lossPrice)
                    pp.append('lossTime',moment(_this.datetime).format("YYYY-MM-DD HH:mm:ss"))
                    pp.append('shopId',_this.$store.state.TOPshopID)
                    if(_this.td[i].remark){
                        pp.append('remark',_this.td[i].remark)
                    }
                    axiosList.push(axios.get('/addReportLoss.do',{params:pp}))
                }
                // console.log('axiosList',axiosList)

                axios.all(axiosList).then(r=>{
                    // console.log('axios.callback',r)
                    if(_this.checkacllback(r)){
                        _this.$message.success('报损成功')
                        _this.$store.state.createbaosun=false
                        _this.close()
                    }else{
                        return
                    }
                })
            }).catch(() => {
                return
            });
        },
        del(i,r){//清空
            this.td=[]
        },
        open(){//打开选择商品的弹窗
            this.$store.commit('ChangeWxGood')
        },
        close(){//清空,关闭当前弹窗
            this.td=[]
            this.datetime=''
            this.$store.state.createbaosunclass=false
        },
        getgooditem(m){//获取选中的商品,请求商品详情
            var _this=this
            this.loading=true
            for(var i=0;i<m.length;i++){
                var pp=new URLSearchParams()
                pp.append('xcxGoodsId',m[i].goodsId)
                axios.get('/getXcxGoodsById.do',{params:pp}).then(r=>{
                    // console.log('getXcxGoodsById',r)
                    if(r.data.code==200){
                        _this.td.push(r.data.response.xcxGoods)
                    }else{
                        _this.$message.error(r.data.message)
                        _this.loading=false
                    }
                })
            }
            _this.loading=false
        }
    }
} 
</script>
<style scoped>
.mgt{
    margin-top: 20px;
}
</style>

